cmake_minimum_required(VERSION 3.10.0)
project(z_UTool VERSION 1.0.2.20250704 LANGUAGES C CXX)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wno-error=deprecated-declarations -Wno-deprecated-declarations -g3 -Wall -Wextra -Werror")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -Wno-error=deprecated-declarations -Wno-deprecated-declarations -g3 -Wall -Wextra -Werror")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(MODULE_SRC_DIR src)
set(MODULE_INC_DIR inc)
set(MODULE_CONFIG_DIR config)

configure_file(
	${CMAKE_SOURCE_DIR}/${MODULE_CONFIG_DIR}/version.rc.in
	${CMAKE_SOURCE_DIR}/${MODULE_CONFIG_DIR}/version.rc
	@ONLY
)

# 只是为了测试提供的宏
add_definitions(-DCURRENT_DIR=${CMAKE_SOURCE_DIR})

if(DEFINED ENV{LINUXROOT})
  set(ENV{PKG_CONFIG_PATH} "$ENV{LINUXROOT}\\lib\\pkgconfig;")
  message(STATUS "<cmake out> ENV{PKG_CONFIG_PATH} is: " $ENV{PKG_CONFIG_PATH})
endif()

include(FindPkgConfig)
pkg_check_modules(PKGS REQUIRED
  tinyxml2
)

find_package(OpenXLSX REQUIRED)

foreach(CFLAG ${PKGS_CFLAGS})
  set(my_pkgs_cflags "${my_pkgs_cflags} ${CFLAG}")
endforeach(CFLAG)

# If using version 3.0 or above, this is necessary.
# Need to remove spaces from flags.
string(STRIP "${my_pkgs_cflags}" my_pkgs_cflags)

foreach(LDFLAG ${PKGS_LDFLAGS})
  set(my_pkgs_ldflags "${my_pkgs_ldflags} ${LDFLAG}")
endforeach(LDFLAG)

# If using version 3.0 or above, this is necessary.
# Need to remove spaces from flags.
string(STRIP "${my_pkgs_ldflags}" my_pkgs_ldflags)
message(STATUS "<debug message>: Link libraries for my_pkgs_ldflags: ${my_pkgs_ldflags}")

foreach(include_dir ${PKGS_INCLUDE_DIRS})
  set(my_pkgs_include_dirs "${my_pkgs_include_dirs} ${include_dir}")
endforeach(include_dir)

# If using version 3.0 or above, this is necessary.
# Need to remove spaces from flags.
string(STRIP "${my_pkgs_include_dirs}" my_pkgs_include_dirs)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${my_pkgs_cflags}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")

file(GLOB_RECURSE
  cxx_sourceFile
  ${CMAKE_SOURCE_DIR}/src/*.cpp
)

include_directories(
  ${CMAKE_SOURCE_DIR}/src
  ${CMAKE_SOURCE_DIR}/src/gcov
  ${CMAKE_SOURCE_DIR}/src/excel
  ${my_pkgs_include_dirs}
)

add_executable(${PROJECT_NAME}
  ${cxx_sourceFile}
  ${MODULE_CONFIG_DIR}/version.rc
)

target_link_libraries(${PROJECT_NAME}
  PUBLIC
  ${my_pkgs_ldflags}
  OpenXLSX::OpenXLSX
)

install(TARGETS ${PROJECT_NAME}
        RUNTIME_DEPENDENCIES
            PRE_EXCLUDE_REGEXES "api-ms-" "ext-ms-"
            POST_EXCLUDE_REGEXES ".*system32/.*\\.dll"
            DIRECTORIES "$ENV{LINUXROOT}/bin"  # 添加MinGW的dll目录
        DESTINATION ${CMAKE_SOURCE_DIR}/bin)
